import java.sql.SQLException;
import java.util.Scanner;

import static java.lang.System.exit;

public class Pra {
    public static void main(String[] args) throws SQLException {
        //mysql  连接，账户，密码
        final  String URL ="JDBC:mysql://localhost:3306/student?usessl=false&serverTimezone=UTC";

        //用户名，用户密码
        final String NAME = "root";
        final String PASS_WORD = "root";
        StuManager stuManager =new StuManager(URL,NAME,PASS_WORD);
        Stu stu = new Stu();

        try {



            //菜单
            while(true) {  //死循环

                System.out.println("1.增加");
                System.out.println("2.删除");
                System.out.println("3.修改");
                System.out.println("4.查询");
                System.out.println("0.退出");
                System.out.println("请输入操作：");

                Scanner sc;
                sc = new Scanner(System.in);
                int ch = sc.nextInt();
                sc.nextLine();


                switch (ch) {
                    //增加学生信息
                    case 1: {
                        System.out.println("————————————————————————增加——————————————————————————————");
                        System.out.println("请输入id：");
                        stu.setId(sc.nextInt());
                        System.out.println("请输入姓名：");
                        stu.setName(sc.next());
                        System.out.println("请输入成绩：");
                        stu.setScore(sc.next());
                        System.out.println("请输入联系方式：");
                        stu.setPhone(sc.next());
                        stuManager.addStu(stu);

                        break;
                    }

                    //删除学生信息
                    case 2: {
                        System.out.println("————————————————————————删除——————————————————————————————");
                        System.out.println("请输入id：");
                        int id = sc.nextInt();
                        System.out.println("---------------请核对以下学生信息：---------------------");
                        stuManager.getStu(id);
                        System.out.println("---------------开始删除---------------------");
                        stuManager.deleteStu(id);

                        break;
                    }

                    //修改学生信息   ————按照学号进行查找，然后修改“score”
                    case 3: {
                        System.out.println("————————————————————————修改——————————————————————————————");
                        System.out.println("请输入id：");
                        int id = sc.nextInt();
                        System.out.println("---------------请核对以下学生信息：---------------------");
                        stuManager.getStu(id);
                        System.out.println("-------------------------------------------------------");
                        System.out.println("请输入想要修改的分数：");
                        String score = sc.next();
                        stuManager.updateStu(id,score);
                        System.out.println("修改之后的信息：");
                        stuManager.getStu(id);

                        System.out.println("**********************修改成功*******************");

                        break;
                    }
                    //查询（按照学号）
                    case 4: {
                        System.out.println("————————————————————————查询——————————————————————————————");
                        System.out.println("请输入学号（按照学号进行查询）：");
                        stuManager.getStu(sc.nextInt());

                        break;
                    }
                    case 0: {
                        exit(0);
                        System.out.println("成功退出！");
                    }

                }
            }


        }catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            stuManager.close();
        }

    }
}
